"use strict"

root = exports ? this

root.furionPush = ((mod)->
  # App 内接受到通知
  foregroundCallback = (data)->
    body = angular.element(document.body)
    rootScope = body.scope().$root

    switch parseInt(data.type)
      when 1 # delegate status change
        url = "#/delegate/detail/#{data.params.delegate_id}"
        if window.location.hash == url # 如果在当前页面就刷新信息
          rootScope.$broadcast('push_notification:delegate-detail', data.params.delegate_id)
        else # 否则在 tab 那有红点通知
          rootScope.$apply ()->
            rootScope.$broadcast('push_notification:delegate', 1)
      when 2 # 通用通知
        rootScope.$broadcast('push_notification:common-notification', data.params.msg)
      # when 3 # 预约通知，进预约交易记录
      #   console.info "预约通知: #{data.params.appointment_id}"
      #   window.location.href = "#/order/records/#{data.params.appointment_id}"
      # when 4 # 变现通知，进变现交易记录
      #   console.info "变现通知: #{data.params.liquidate_id}"
      #   window.location.href = "#/cash/records/#{data.params.liquidate_id}"
      else
        console.info "不支持此类型的通知: #{data.type}"

  # 通知栏点击进去
  backgroundCallback = (data)->
    if data.params.url
      window.location.href = data.params.url
    else
      window.location.href = "#/app/invest"
    return
    # switch parseInt(data.type)
    #   when 1 # delegate status change
    #     url = "#/delegate/detail/#{data.params.delegate_id}"
    #     window.location.href = url
    #     rootScope.$broadcast('push_notification:delegate-detail', data.params.delegate_id)
    #   when 2 # 通用通知，进主页
    #     console.info "通用通知: #{data.params.msg}"
    #     window.location.href = "#/tab/licai/assembly"
    #   when 3 # 预约通知，进预约交易记录
    #     console.info "预约通知: #{data.params.appointment_id}"
    #     window.location.href = "#/order/records/#{data.params.appointment_id}"
    #   when 4 # 变现通知，进变现交易记录
    #     console.info "变现通知: #{data.params.liquidate_id}"
    #     window.location.href = "#/cash/records/#{data.params.liquidate_id}"
    #   when 5 # 指数通知，进指数宝
    #     console.info "指数通知: #{data.params.msg}"
    #     window.location.href = "#/tab/licai/guess"
    #   when 6 # 资产通知，进资产页
    #     console.info "资产通知: #{data.params.msg}"
    #     window.location.href = "#/tab/asset"
    #   else
    #     console.info "不支持此类型的通知: #{data.type}"
    #     window.location.href = "#/tab/licai/assembly"

  mod.jpush = {}

  mod.jpush.hasPlugin = ->
    window.plugins && window.plugins.jPushPlugin

  # 设置别名(用户标识)
  mod.jpush.setAlias = ->
    return if !mod.jpush.hasPlugin()

    if furion.storage.isLogin()
      window.plugins.jPushPlugin.setAlias furion.storage.account().phone
    else
      window.plugins.jPushPlugin.setAlias ""

  mod.jpush.getRegistrationID = ->
    return if !mod.jpush.hasPlugin()

    # body = angular.element(document.body)
    # rootScope = body.scope().$root
    # rootScope.$apply ()->
    #   rootScope.$broadcast('jpush-registration', registration_id)
    window.plugins.jPushPlugin.isPushStopped (result)->
      if result != 0
        window.plugins.jPushPlugin.resumePush()
      window.plugins.jPushPlugin.getRegistrationID (registration_id)->
        console.log "jpush registration id: #{registration_id}"
        furion.storage.updateDeviceInfo({jpush_token: registration_id}) if registration_id? && registration_id != ""

  mod.jpush.init = (debugMode)->
    return if !mod.jpush.hasPlugin()
    # return unless ionic.Platform.isIOS() || ionic.Platform.isAndroid() # 目前只支持 ios/android 在此插件下使用

    window.plugins.jPushPlugin.init()
    window.plugins.jPushPlugin.setBasicPushNotificationBuilder()
    # window.plugins.jPushPlugin.setCustomPushNotificationBuilder()

    if ionic.Platform.isIOS()
      window.plugins.jPushPlugin.setDebugModeFromIos() if debugMode
      window.plugins.jPushPlugin.setApplicationIconBadgeNumber(0)

    if ionic.Platform.isAndroid()
      window.plugins.jPushPlugin.setDebugMode(debugMode)
      window.plugins.jPushPlugin.setStatisticsOpen(true)

    mod.jpush.getRegistrationID()

    window.plugins.jPushPlugin.openNotification = (data)->
      console.info 'notificationCallback'
      r = JSON.parse(data)
      if ionic.Platform.isAndroid()
        extra = r.extras['cn.jpush.android.EXTRA']
        backgroundCallback(extra)
        return

      if ionic.Platform.isIOS()
        e = r.aps

        if e.sound
          console.log "sound #{e.sound}"
          # #playing a sound also requires the org.apache.cordova.media plugin
          # snd = new Media(e.sound)
          # snd.play()

        # if e.badge
        #   window.plugins.jPushPlugin.setApplicationIconBadgeNumber(e.badgee
        window.plugins.jPushPlugin.setApplicationIconBadgeNumber(0)

        extra = {type: e.type, params: JSON.parse(e.params)}
        if e.foreground == "1" # 如果在app内打开
          foregroundCallback(extra)
        else
          backgroundCallback(extra)
        return

    window.plugins.jPushPlugin.receiveMessage = (data)->
      console.info 'messageCallback'
      r = JSON.parse(data)
      if ionic.Platform.isAndroid()
        extra = r.extras['cn.jpush.android.EXTRA']

      else if ionic.Platform.isIOS()
        window.plugins.jPushPlugin.setApplicationIconBadgeNumber(0)
        extra = r.extras

      foregroundCallback(extra)

    mod.jpush.setAlias()

    onPause = ()->
      if furionPush.jpush.hasPlugin() && ionic.Platform.isIOS()
        window.plugins.jPushPlugin.setApplicationIconBadgeNumber(0)
      # window.plugins.jPushPlugin.onPause()
      console.log("APP onPause")
    onResume = ()->
      if furionPush.jpush.hasPlugin() && ionic.Platform.isIOS()
        window.plugins.jPushPlugin.setApplicationIconBadgeNumber(0)
      # window.plugins.jPushPlugin.onResume()
      console.log("APP onResume")

    document.addEventListener('pause', onPause)
    document.addEventListener('resume', onResume)

  return mod
)(window.furionPush || {})
